//
//  ViewController.m
//  MKUITestApp
//
//  Created by sushangjin on 2020/6/1.
//  Copyright © 2020 sushangjin. All rights reserved.
//

#import "TableViewController.h"
#import "CollectionViewController.h"
#import "ItemTVCell2.h"
#import "ItemTVCell.h"

@interface TableViewController ()
@property(nonatomic,strong) MKUITableView *tableView;
@end

@implementation TableViewController
+ (MKTFuncItem *)funcItem{
    return [MKTFuncItem funcWithName:@"TableView" viewControllerClass:self];
}
- (id)init{
    if(self=[super init]){
        self.tabBarItem.title = @"Table";
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"Table";
    self.tableView = [[MKUITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    //
    [self __reloadData];
    UIBarButtonItem *refreshItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(__onRefresh:)];
    
    self.navigationItem.rightBarButtonItems = @[refreshItem];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}
- (void)__onRefresh:(id)sender{
    [self.tableView.model reloadTableViewData];
}
- (void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    CGRect fullBounds = self.view.bounds;
    CGRect bounds = fullBounds;
    if (@available(iOS 11.0, *)) {
        bounds = self.view.safeAreaLayoutGuide.layoutFrame;
    } else {
    }
    self.tableView.frame = bounds;
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.tableView deselectRowAtIndexPath:self.tableView.indexPathForSelectedRow animated:YES];
}
- (void)__reloadData{
    [self.tableView.model removeAllSectionModels];
    for (int i=0; i<10000; i++) { 
        MKUITableViewCellModel *cm;
        if(i%2==0){
            cm = [MKUITableViewCellModel modelWithValue:[[Item alloc] initWithName:[NSString stringWithFormat:@"Table_%@",@(i)]] cellClass:[ItemTVCell class] whenClick:^(__kindof MKUITableViewCellModel * _Nonnull cm) {
                [cm.tableView.mk_navigationControllerOfFirst pushViewController:[TableViewController new] animated:YES];
            }];
        }else{
            cm = [MKUITableViewCellModel modelWithValue:[[Item alloc] initWithName:[NSString stringWithFormat:@"Collection_%@",@(i)]] cellClass:[ItemTVCell2 class] whenClick:^(__kindof MKUITableViewCellModel * _Nonnull cm) {
                [cm.tableView.mk_navigationControllerOfFirst pushViewController:[CollectionViewController new] animated:YES];
            }];
        }
        cm.whenClickAccessory = cm.whenClick;
        cm.whenDelete = ^void(MKUITableViewCellModel *cm) {
            [cm removeFromModelWithAnimated:YES];
        };
        cm.canEdit = YES;
        [self.tableView.model addCellModel:cm];
    }
    [self.tableView.model reloadTableViewData];
}

@end
